package com.zdb.java.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ShareDataPC {

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private volatile List<String> list = new ArrayList<>(3);

    public void produce() throws Exception {

        lock.lock();
        try {
            while (list.size() == 3) {
                condition.await();
            }
            String str = UUID.randomUUID().toString().substring(0, 8);
            list.add(str);
            System.out.println("+++++++++ produce:" + str);
            condition.signalAll();
        } finally {
            lock.unlock();
        }

    }

    public void consume() throws Exception {
        lock.lock();
        try {
            while (list.size() == 0) {
                condition.await();
            }
            String str = list.remove(list.size()-1);
            System.out.println("------ consume: " + str);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

}

class ShareDataPC2 {

    private Lock lock = new ReentrantLock();
    private Condition prodCondition = lock.newCondition();
    private Condition consumeCondition = lock.newCondition();
    private volatile List<String> list = new ArrayList<>(3);

    public void produce() throws Exception {

        lock.lock();
        try {
            while (list.size() == 3) {
                prodCondition.await();
            }
            String str = UUID.randomUUID().toString().substring(0, 8);
            list.add(str);
            System.out.println("+++++++++ produce:" + str);
            consumeCondition.signalAll();
        } finally {
            lock.unlock();
        }

    }

    public void consume() throws Exception {
        lock.lock();
        try {
            while (list.size() == 0) {
                consumeCondition.await();
            }
            String str = list.remove(list.size()-1);
            System.out.println("------ consume: " + str);
            prodCondition.signalAll();
        } finally {
            lock.unlock();
        }
    }

}

public class ProducerConsumerDemo {
    public static void main(String[] args) throws Exception {

//        ShareDataPC data = new ShareDataPC();
        ShareDataPC2 data = new ShareDataPC2();

        for (int j = 0; j <5; j++) {
            new Thread(() -> {
                for (int i = 0; i < 10; i++) {
                    try {
                        data.produce();
                        TimeUnit.SECONDS.sleep(2);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, "p" + j).start();
            new Thread(() -> {
                for (int i = 0; i < 10; i++) {
                    try {
                        data.consume();
                        TimeUnit.SECONDS.sleep(2);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, "c" + j).start();
        }


    }
}
